package packetsender;

import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;

@SuppressWarnings("serial")
public class MyList extends List {
	public MyList(final PacketSender p) {
		loadAllRes();
		this.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				File f = new File("res/" + e.getActionCommand());
				System.out.println(f);

				try {
					p.txtTesttxt.setText(e.getActionCommand());
					BufferedReader b = new BufferedReader(new FileReader(f));
					p.txtrA.setText("");
					while(true) {
						String tem = b.readLine();
						if(tem == null) {
							break;
						}
						p.txtrA.append(tem + "\n");
					}

				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
	}

	private void loadAllRes() {
		File f = new File("res");

		for (String fName : f.list()) {
			this.add(fName);
		}
		System.out.println(Arrays.toString(f.list()));
	}

	public void update() {
		this.removeAll();
		loadAllRes();
	}

	public void delete(String s) {
		File f = new File("res" + s);
		System.out.println(f);
		f.delete();
		update();
	}

	public void deleteSelect() {
		File f = new File("res/" + this.getSelectedItem());
		System.out.println(f);
		f.delete();
		update();
	}
}
